package top.liuziqian.web.controller.shop.goods;




import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.liuziqian.common.annotation.Log;
import top.liuziqian.common.core.controller.BaseController;
import top.liuziqian.common.core.domain.AjaxResult;
import top.liuziqian.common.core.page.TableDataInfo;
import top.liuziqian.common.enums.BusinessType;
import top.liuziqian.shop.domain.GoodsSupplier;
import top.liuziqian.shop.service.IGoodsSupplierCategoryService;
import top.liuziqian.shop.service.IGoodsSupplierService;

import java.util.List;


@Controller
@RequestMapping("/goods/supplier")
@Api("商品供应商管理")
public class SupplierController extends BaseController {

    private String prefix = "shop/goods/supplier";

    @Autowired
    private IGoodsSupplierService goodsSupplierService;

    @Autowired
    private IGoodsSupplierCategoryService goodsSupplierCategoryService;

    @RequiresPermissions("goods:supplier:view")

    @GetMapping()
    public String supplier()
    {
        return prefix + "/supplier";
    }


    /**
     * 新增供应商
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //查询所有分类
        mmap.put("categorys",goodsSupplierCategoryService.selectSupplierCategoryAll());
        return prefix + "/add";
    }

    @GetMapping("/edit/{supplierId}")
    public String edit(@PathVariable("supplierId") Long supplierId, ModelMap mmap)
    {
        mmap.put("supplier",goodsSupplierService.selectSupplierById(supplierId));
        mmap.put("categorys",goodsSupplierCategoryService.selectSupplierCategoryAll());
        return prefix + "/edit";
    }

    /**
     * 查询供应商列表
     */
    @RequiresPermissions("goods:supplier:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("获取商品供应商")
    public TableDataInfo list(GoodsSupplier goodsSupplier)
    {
        startPage();
        List<GoodsSupplier> list = goodsSupplierService.selectSupplierList(goodsSupplier);
        return getDataTable(list);
    }


    /**
     * 新增供应商
     */
    @RequiresPermissions("goods:supplier:add")
    @Log(title = "商品供应商", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated GoodsSupplier goodsSupplier)
    {
        goodsSupplier.setCreateBy(getLoginName());
        return toAjax(goodsSupplierService.insertSupplier(goodsSupplier));
    }

    @RequiresPermissions("goods:supplier:edit")
    @Log(title = "商品供应商", businessType = BusinessType.INSERT)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated GoodsSupplier goodsSupplier)
    {
        goodsSupplier.setUpdateBy(getLoginName());
        return toAjax(goodsSupplierService.updateSupplier(goodsSupplier));
    }

    /**
     * 删除供应商
     */
    @RequiresPermissions("goods:supplier:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(goodsSupplierService.deleteSupplierByIds(ids));
    }
}

